package com.kakarote.hrm.controller;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.UploadEntity;
import com.kakarote.hrm.entity.BO.QueryRecruitProcesPageListBO;
import com.kakarote.hrm.entity.PO.HrmRecruitProces;
import com.kakarote.hrm.entity.VO.RecruitProcesVO;
import com.kakarote.hrm.service.IHrmRecruitProcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 招聘过程 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/hrmRecruitProces")
@Api(tags = "招聘管理-招聘过程")
public class HrmRecruitProcesController {

    @Autowired
    private IHrmRecruitProcesService recruitProcesService;

    @ApiOperation("添加招聘过程")
    @PostMapping("/addRecruitProces")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.SAVE)
    public Result addRecruitProces(@Validated @RequestBody HrmRecruitProces recruitProces) {
        OperationLog operationLog = recruitProcesService.addRecruitProces(recruitProces);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("修改招聘过程")
    @PostMapping("/setRecruitProces")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.UPDATE)
    public Result setRecruitProces(@Validated @RequestBody HrmRecruitProces recruitProces) {
        OperationLog operationLog = recruitProcesService.setRecruitProces(recruitProces);
        return OperationResult.ok(operationLog);
    }

    /**
     * 获取招聘过程详情
     *
     * @param procesId 过程id
     */
    @ApiOperation("获取招聘过程详情")
    @PostMapping("/queryById/{procesId}")
    public Result<RecruitProcesVO> queryById(@PathVariable("procesId") Long procesId) {
        RecruitProcesVO recruitProcesVO = recruitProcesService.queryById(procesId);
        return Result.ok(recruitProcesVO);
    }

    @ApiOperation("查询招聘过程列表")
    @PostMapping("/queryRecruitProcesPageList")
    public Result<BasePage<RecruitProcesVO>> queryRecruitProcesPageList(@RequestBody QueryRecruitProcesPageListBO queryRecruitProcesPageListBO) {
        BasePage<RecruitProcesVO> page = recruitProcesService.queryRecruitProcesPageList(queryRecruitProcesPageListBO);
        return Result.ok(page);
    }

    @ApiOperation("根据计划id查询所有招聘过程列表")
    @PostMapping("/queryByPlanId/{planId}")
    public Result<List<RecruitProcesVO>> queryByPlanId(@PathVariable("planId") Long planId) {
        List<RecruitProcesVO> procesList = recruitProcesService.queryByPlanId(planId);
        return Result.ok(procesList);
    }

    @ApiOperation("根据记录id查询所有招聘过程列表")
    @PostMapping("/queryByRecordId/{recordId}")
    public Result<List<RecruitProcesVO>> queryByRecordId(@PathVariable("recordId") Long recordId) {
        List<RecruitProcesVO> procesList = recruitProcesService.queryByRecordId(recordId);
        return Result.ok(procesList);
    }

    @ApiOperation("删除招聘过程")
    @PostMapping("/deleteRecruitProces/{procesId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.DELETE)
    public Result deleteRecruitProces(@PathVariable("procesId") Long procesId) {
        OperationLog operationLog = recruitProcesService.deleteRecruitProces(procesId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("批量删除招聘过程")
    @PostMapping("/batchDeleteRecruitProces")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.DELETE)
    public Result batchDeleteRecruitProces(@RequestBody List<Long> procesIds) {
        OperationLog operationLog = recruitProcesService.batchDeleteRecruitProces(procesIds);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据计划id删除所有招聘过程")
    @PostMapping("/deleteByPlanId/{planId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.DELETE)
    public Result deleteByPlanId(@PathVariable("planId") Long planId) {
        OperationLog operationLog = recruitProcesService.deleteByPlanId(planId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据记录id删除所有招聘过程")
    @PostMapping("/deleteByRecordId/{recordId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.DELETE)
    public Result deleteByRecordId(@PathVariable("recordId") Long recordId) {
        OperationLog operationLog = recruitProcesService.deleteByRecordId(recordId);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/exportRecruitProces")
    @ApiOperation("导出招聘过程")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PROCES, behavior = BehaviorEnum.EXCEL_EXPORT, type = OperateTypeEnum.EXPORT)
    public void exportRecruitProces(@RequestBody QueryRecruitProcesPageListBO queryRecruitProcesPageListBO, HttpServletResponse response) throws IOException {
        recruitProcesService.exportRecruitProces(response, queryRecruitProcesPageListBO);
    }

    @PostMapping("/downloadExcel")
    @ApiOperation("获取导入模板")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        recruitProcesService.downloadExcel(response);
    }

    @PostMapping("/excelImport")
    @ApiOperation("excel导入招聘过程")
    @OperateLog(apply = ApplyEnum.HRM, behavior = BehaviorEnum.EXCEL_IMPORT, type = OperateTypeEnum.IMPORT, object = OperateObjectEnum.HRM_RECRUIT_PROCES)
    public Result<JSONObject> excelImport(@RequestParam("file") MultipartFile file) {
        JSONObject object = recruitProcesService.excelImport(file);

        UploadEntity uploadEntity = recruitProcesService.uploadTempFile(file, null);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject("uploadData", uploadEntity.getFileId());
        operationLog.setOperationObject("name", "招聘过程");

        JSONObject info = new JSONObject();
        info.put("count", object.getInteger("totalSize"));
        info.put("errorSize", object.getInteger("errSize"));
        info.put("errorFile", object.getString("token"));
        operationLog.setOperationInfo(info.toJSONString());
        return OperationResult.ok(object, ListUtil.toList(operationLog));
    }
}

